import Mock from 'mockjs';

// 模拟管理员数据
const adminList = Mock.mock({
  'lists|30': [
    {
      'id|+1': 1,
      'username': '@string',
      'password': '@string',
      'account': '@email',
      'tel': /^1[3456789]\d{9}$/,
      'role|1': ['超级管理员', '管理员', '普通用户'],
      'isEnable|1': [0,1],
      'time': '@datetime',
    }
  ]
})
// 调取管理员数据接口
Mock.mock(/\api\/getAdminList/, 'get', (options: any) => {
  // 解析查询参数
  const params = new URLSearchParams(options.url.split('?')[1])
  const pageNum = parseInt(params.get('pageNum') as any) || 1
  const pageSize = parseInt(params.get('pageSize') as any) || 5

  // 计算分页数据
  const start = (pageNum - 1) * pageSize
  const end = start + pageSize
  const pageData = adminList.lists.slice(start, end)
  return {
    code: 200,
    message: '获取数据成功',
    data: {
      total: adminList.lists.length,
      list: pageData,
      pageNum,
      pageSize,
    },
  }
})
// 添加管理员接口
Mock.mock(/\api\/addAdmin/, 'post', (options: any) => { 
  const params = JSON.parse(options.body)
  adminList.lists.push(params)
  return {
    code: 200,
    message: '添加成功',
  }
})
// 删除管理员接口
Mock.mock(/\api\/deleteAdmin/, 'delete', (options: any) => { 
  // console.log('options',options)
  const params = options.url.split('?')[1].split('=')[1]
  adminList.lists = adminList.lists.filter((item: any) => item.id != params)
  return {
    code: 200,
    message: '删除成功',
  }
})
// 编辑管理员接口
Mock.mock(/\api\/editAdmin/, 'put', (options: any) => { 
  const params = JSON.parse(options.body)
  adminList.lists = adminList.lists.map((item: any) => {
    if (item.id == params.id) {
      return params
    }
    return item
  })
  return {
    code: 200,
    message: '编辑成功',
  }
})